//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';

export default {
  name:'',
  //import引入的组件需要注入到对象中才能使用
  components: {},
  data() {
  return {
    formQuery:{
      id:'',
      relation_name:'',
      birthday:'',
      sex:'',
      school:'',
      school_class:'',
      province_id:'',
      city_id:'',
      district_id:'',
      street_id:'',
      address:''
    },
    dyTitle:'',
    iPnoneBottomBol: false, //是否是苹果手机
    sexShow:false,
    radio:'1',
    minDate: new Date(1970, 0, 1),
    maxDate: new Date(2050, 12, 1),
    currentDate: new Date(),
    dateShow:false,
    addressShow:false,
    strShow:false,
    streetShow:false,
    is_region:'',
    region_hide:'',
     //街道
     area: district,
     addressName:'',
     streetName:'',
     host:'',
     sexName:'',
     sexChecked:false,
     girlChecked:false
  };
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    initData() {
      this.strShow = false;
      this.addressShow = false;
      this.formQuery.relation_name = '';
      this.formQuery.birthday = '';
      this.currentDate = new Date();
      this.formQuery.sex = '';
      this.sexName = '';
      this.formQuery.school = '';
      this.formQuery.school_class = '';
      this.formQuery.province_id = '';
      this.formQuery.city_id = '';
      this.formQuery.district_id = '';
      this.formQuery.street_id = '';
      this.formQuery.address = '';
      this.streetName = '';
      this.addressName = '';
      this.sexChecked = false;
      this.girlChecked = false;
    },
    getiPnoneBottom() { //检测是否为苹果手机
      var userAgent = navigator.userAgent;
      var isiOS = !!userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
      isiOS?this.iPnoneBottomBol = true:this.iPnoneBottomBol = false;
    },
    //学员管理详情
    goodsDetail() {
      $http.get("plugin.event-registration.api.student.edit", {id:this.formQuery.id}, "正在获取").then(
        response => {
          if (response.result == 1) {
            let res = response.data;
            this.formQuery.relation_name = res.relation_name;
            this.formQuery.birthday = res.birthday;
            this.currentDate = new Date(res.birthday);
            this.formQuery.sex = res.sex;
            this.sexName = res.sex == 1?'男':'女';
            this.formQuery.school = res.school;
            this.formQuery.school_class = res.school_class;
            this.formQuery.province_id = res.province_id;
            this.formQuery.city_id = res.city_id;
            this.formQuery.district_id = res.district_id;
            this.formQuery.street_id = res.street_id;
            this.formQuery.address = res.address;
            this.addressName = res.province + res.city + res.district;
            if(res.street_id) {
              this.streetName = res.street;
              this.strShow = true;
            }

          } else {
            
            this.$dialog.alert({ message: response.msg });
          }
        },
        function(response) {
          // console.log(response);
          // error callback
        }
      );
    },
    studentConfirm() {
      if (this.fun.isTextEmpty(this.formQuery.relation_name)) {
        this.$toast("请填写学生姓名");
        return;
      }
      if (this.fun.isTextEmpty(this.formQuery.birthday)) {
        this.$toast("请选择出生日期");
        return;
      }
      if (this.fun.isTextEmpty(this.formQuery.sex)) {
        this.$toast("请选择性别");
        return;
      }
      if (this.fun.isTextEmpty(this.formQuery.school)) {
        this.$toast("请填写就读学校名称");
        return;
      }
      if (this.fun.isTextEmpty(this.formQuery.school_class)) {
        this.$toast("请填写班级名称");
        return;
      }
      if (this.fun.isTextEmpty(this.addressName)) {
        this.$toast("请选择地区");
        return;
      }
      if (this.fun.isTextEmpty(this.streetName)) {
        this.$toast("请选择街道");
        return;
      }
      if (this.fun.isTextEmpty(this.formQuery.address)) {
        this.$toast("请填写详细地址");
        return;
      }
      $http.post("plugin.event-registration.api.student.edit-post", this.formQuery, "正在获取").then(
        response => {
          if (response.result == 1) {
            let res = response.data;
            this.$toast(response.msg);
            setTimeout(() => {
              this.goBack();
            },500);
            
          } else {
            
            this.$dialog.alert({ message: response.msg });
          }
        },
        function(response) {
          // console.log(response);
          // error callback
        }
      );
    },
    goBack() {
      if(window.history.length <= 1) {
        this.$router.push(this.fun.getUrl("home", {}));
      } else {
        this.$router.go(-1);
      }
    },
    dateCancel() {
      this.dateShow = false;
    },
    dateConfirm(event) {
      this.formQuery.birthday = this.timestampToTime(Date.parse(event));
      this.dateShow = false;
    },
    addressCallback(obj) {
      this.addressName = obj.itemName1 + " " + obj.itemName2 + " " + obj.itemName3;
      this.formQuery.province_id = obj.itemValue1;
      this.formQuery.city_id = obj.itemValue2;
      this.formQuery.district_id = obj.itemValue3;
      this.formQuery.street_id = '';
      this.streetName = '';
      this.getStreet(obj.itemValue3);
    },
    //后台地址的开关控制
    getIsOpenAddress() {
      $http
        .get("member.member-address.is-region")
        .then(response => {
          if (response.result == 1) {
            let data = response.data;
            this.is_region = data.is_region;
            this.region_hide = data.region_hide;
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    streetShowbtn() {
      if(this.is_region == 1 &&  this.fun.isTextEmpty(this.formQuery.district_id)) {
        this.$dialog.alert({ message: "请先选择所在地区" });
      } else {
        this.streetShow = true;
        this.getStreet(this.formQuery.district_id);
        // this.$toast('请先选择地区');
      }
    },
    treetShowLeft() {
      this.streetShow = false;
    },
    getStreet(param) {
      $http
        .get("member.memberAddress.street", { district_id: param })
        .then(response => {
          if (response.result === 1) {
            if (!this.fun.isTextEmpty(response.data) && response.data.length > 0) {
              this.districtVal = response.data;
              this.strShow = true;
            } else {
              this.strShow = false;
            }
          } else {
            this.strShow = false;
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    streetConfirm(item) {
      this.streetName = item.areaname;
      this.formQuery.street_id = item.id;
      this.streetShow = false;
    },
    sexChange(type) {
      if(type == 'male') {
        this.sexChecked = true;
        this.girlChecked = false;
        this.formQuery.sex= 1;
        this.sexName = '男';
      } else {
        this.girlChecked = true;
        this.sexChecked = false;
        this.formQuery.sex= 2;
        this.sexName = '女';
      }
      this.sexShow = false;
    },
    //将时间戳转换成正常时间格式
    timestampToTime: function(timestamp, key) {
      if (!Number(timestamp) || timestamp == "") {
        return "";
      }
      let date = new Date(timestamp); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
      let Y = date.getFullYear() + "-";
      let M = (date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1) + "-";
      let D = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
      return Y + M + D;
    },
    sexPopColse() {
      this.sexShow = false;
    },
    dateCancel() {
      this.dateShow = false;
    },
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {
  
  },
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
  
  },
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {
    this.getiPnoneBottom();
    this.getIsOpenAddress();
    this.host = JSON.parse(localStorage.getItem("yz_basic_info")).host || "";
    if (this.$route.query.id) {
      this.formQuery.id = Number(this.$route.query.id);
      this.goodsDetail();
      this.dyTitle = '学员编辑';
      this.fun.setWXTitle('学员编辑');
    } else {
      this.dyTitle = '新增学员';
      this.fun.setWXTitle('新增学员');
      this.initData();
    }
    
  }, //如果页面有keep-alive缓存功能，这个函数会触发
}